package demo.bean.handler;




import demo.bean.BeanFactory;
import demo.bean.InjectHandler;
import demo.bean.annotations.Inject;

import java.lang.reflect.Field;

//属性注入
public class FieldInjectHandler implements InjectHandler {

    public void execute(Object target, Class<?> targetClass, BeanFactory factory) throws IllegalAccessException, InstantiationException {
        Field[] fields = targetClass.getDeclaredFields();
        for (Field field : fields){
            if (field.isAnnotationPresent(Inject.class)){
                field.setAccessible(true);
                String value = field.getAnnotation(Inject.class).value();
                Object bean = factory.getBean(value);
                field.set(target,bean);
            }
        }
    }
}
